Imports System.Data
Imports Telerik.Web.UI
'Imports Telerik.WebControls
Partial Class Common_UserControls_OneFilteredDropDownList
    Inherits System.Web.UI.UserControl

    Public Event SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs)

    Public Enum DropDownDefaultItem
        ChooseOne
        ChooseAll
    End Enum

    Public Enum DropDownNothingItem
        ChooseNothing
    End Enum

    Protected Overridable Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
        RaiseEvent SelectedIndexChanged(Me, e)
    End Sub

    Public Property DataSourceId() As String
        Get
            Return rcbd.DataSourceID
        End Get
        Set(ByVal value As String)
            rcbd.DataSourceID = value
        End Set
    End Property

    <ComponentModel.Browsable(False)> _
    Public Property SelectedValue() As String
        Get
            Return rcbd.SelectedValue
        End Get
        Set(ByVal value As String)

            If rcbd.FindItemByValue(value) Is Nothing Or value = "-2" Then
                rcbd.SelectedValue = "-1"
            Else
                rcbd.SelectedValue = value
            End If
        End Set
    End Property

    <ComponentModel.Browsable(False)> _
    Public ReadOnly Property SelectedText() As String
        Get
            Return rcbd.SelectedItem.Text
        End Get
    End Property

    <ComponentModel.Browsable(False)> _
    Public Property SelectedIndex() As Integer
        Get
            Return rcbd.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            rcbd.SelectedIndex = value
        End Set
    End Property

    Public Property DataValueField() As String
        Get
            Return rcbd.DataValueField
        End Get
        Set(ByVal value As String)
            rcbd.DataValueField = value
        End Set
    End Property

    Public Property DataTextField() As String
        Get
            Return rcbd.DataTextField
        End Get
        Set(ByVal value As String)
            rcbd.DataTextField = value
        End Set
    End Property


    Public WriteOnly Property DefaultForeColor() As String
        Set(ByVal value As String)
            If Not rcbd.FindItemByValue(value) Is Nothing Then
                rcbd.FindItemByValue(value).ForeColor = Drawing.Color.Red
            End If
        End Set
    End Property

    Public Property AutoPostBack() As Boolean
        Get
            Return rcbd.AutoPostBack
        End Get
        Set(ByVal value As Boolean)
            rcbd.AutoPostBack = value
            'rcbd.AutoPostBack = value
        End Set
    End Property

    Public Property NoWrap() As Boolean
        Get
            Return rcbd.NoWrap
        End Get
        Set(ByVal value As Boolean)
            rcbd.NoWrap = value

        End Set
    End Property

    Public Property IsRequired() As Boolean
        Get
            Return CVRadComboBoxValue.Enabled
        End Get
        Set(ByVal value As Boolean)
            CVRadComboBoxValue.Enabled = value
        End Set
    End Property

    Public Property ValidationGroup() As String
        Get
            Return CVRadComboBoxValue.ValidationGroup
        End Get
        Set(ByVal value As String)
            CVRadComboBoxValue.ValidationGroup = value
            rcbd.ValidationGroup = value
        End Set
    End Property

    Public Property Enabled() As Boolean
        Get
            Return rcbd.Enabled
        End Get
        Set(ByVal value As Boolean)
            rcbd.Enabled = value
        End Set
    End Property

    Public Property CausesValidation() As Boolean
        Get
            Return rcbd.CausesValidation
        End Get
        Set(ByVal value As Boolean)
            rcbd.CausesValidation = value
        End Set
    End Property

    Public WriteOnly Property DefaultChoosedItem() As DropDownDefaultItem
        Set(ByVal value As DropDownDefaultItem)

            Dim strDefaultText As String
            Dim NothingValueItem As New RadComboBoxItem
            Dim NothingDescriptionItem As New RadComboBoxItem
            If value = DropDownDefaultItem.ChooseOne Then
                strDefaultText = GetGlobalResourceObject("Controls", "DDLItemChoose")

                NothingValueItem.Text = ""
                NothingDescriptionItem.Text = ""

                NothingValueItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
                NothingDescriptionItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")

                NothingValueItem.Enabled = False
                NothingDescriptionItem.Enabled = False
            Else
                strDefaultText = GetGlobalResourceObject("Controls", "DDLItemAll")

                NothingValueItem.Text = GetGlobalResourceObject("Controls", "DDLItemNothing")
                NothingDescriptionItem.Text = GetGlobalResourceObject("Controls", "DDLItemNothing")

                NothingValueItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
                NothingDescriptionItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
            End If
            rcbd.Items.Add(NothingDescriptionItem)

            Dim DefaultValueItem As New RadComboBoxItem
            Dim DefaultDescriptionItem As New RadComboBoxItem

            DefaultValueItem.Text = strDefaultText
            DefaultDescriptionItem.Text = strDefaultText

            DefaultValueItem.Value = GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
            DefaultDescriptionItem.Value = GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
            DefaultValueItem.Selected = True
            DefaultDescriptionItem.Selected = True

            'RadComboBoxValue.Items.Add(DefaultValueItem)
            rcbd.Items.Add(DefaultDescriptionItem)
        End Set
    End Property



    'This Code is Added ToPrevent Duplicate the recprds each time the ComboBox is rebined
    Protected Sub rcbd_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbd.DataBinding
        ClearAndAddDefaultItem(rcbd)
    End Sub



    Private Sub ClearAndAddDefaultItem(ByRef comboBox As RadComboBox)
        Dim defaultItem As New RadComboBoxItem
        Dim NothingItem As New RadComboBoxItem

        NothingItem = comboBox.FindItemByValue("-2")
        defaultItem = comboBox.FindItemByValue("-1")
        comboBox.Items.Clear()

        If Not IsNothing(defaultItem) Then
            comboBox.Items.Add(New RadComboBoxItem(defaultItem.Text, defaultItem.Value))
        End If
        If Not IsNothing(NothingItem) Then
            comboBox.Items.Add(New RadComboBoxItem(NothingItem.Text, NothingItem.Value))
        End If

    End Sub

    Protected Sub rcbd_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbd.DataBound
        Dim defaultItem As New RadComboBoxItem
        Dim NothingItem As New RadComboBoxItem

        NothingItem = rcbd.FindItemByValue("-2")
        defaultItem = rcbd.FindItemByValue("-1")
        defaultItem.Selected = True
    End Sub

    Protected Sub rcbd_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles rcbd.SelectedIndexChanged
        OnSelectedIndexChanged(e)
    End Sub
End Class
